連結:https://leetcode.com/problems/valid-parentheses/description/
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
for (int i = 0 ; i < s.length();i++)
{
char c = s.charAt(i);
if (c == '(')
stack.push(')');
else if (c == '{')
stack.push('}');
else if (c == '[')
stack.push(']');
else if (stack.isEmpty() || stack.pop() != c)
return false;
}
if (stack.isEmpty())
return true;
else
return false;
}
}
連結:https://leetcode.com/problems/valid-parentheses/description/
class MinStack {
private Stack<Integer> stack;
private int min = Integer.MAX_VALUE;
public MinStack() {
stack = new Stack<Integer>();
}
public void push(int val) {
if (val <= min)
{
stack.push(min);
min = val;
}
stack.push(val);
}
public void pop() {
if (stack.pop() == min)
min = stack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return min;
}
}